package cn.antho.product.service.impl;

import cn.antho.basic.util.BaiduAuditUtils;
import cn.antho.basic.util.FreemarkHtml;
import cn.antho.basic.util.LoginContext;
import cn.antho.org.domain.Employee;
import cn.antho.org.mapper.EmployeeMapper;
import cn.antho.org.service.IEmployeeService;
import cn.antho.pet.domain.Pet;
import cn.antho.pet.domain.PetOnsaleAuditLog;
import cn.antho.product.domain.Product;
import cn.antho.product.domain.ProductDetail;
import cn.antho.product.domain.ProductOnsaleAuditLog;
import cn.antho.product.mapper.ProductDetailMapper;
import cn.antho.product.mapper.ProductMapper;
import cn.antho.product.mapper.ProductOnsaleAuditLogMapper;
import cn.antho.product.service.IProductDetailService;
import cn.antho.product.service.IProductService;
import cn.antho.basic.service.impl.BaseServiceImpl;
import cn.antho.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author antho
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductOnsaleAuditLogMapper productOnsaleAuditLogMapper;
    @Autowired
    private IProductDetailService productDetailService;
    @Value("${pagedir}")
    private String path;

    @Override
    public Product getProductDetail(Long id) {
        return productMapper.getProductDetail(id);
    }

    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) throws Exception {
        Map<Long, Product > map = productMapper.findAll().stream().filter(product -> product.getState() == 0).collect(Collectors.toMap(Product::getId, product -> product));
        Date date = new Date();
        String productPath = path + "\\productDetail";
        for (Long id : ids) {
            Product product = map.get(id);
            StringBuilder sb = new StringBuilder();
            // 不等于null 即数据本身是下架的
            if (null != product) {
                if (!BaiduAuditUtils.TextCensor(product.getName())) {
                    sb.append("服务上架百度AI审核名称失败！服务名称为：" + product.getName() + ";");
                }
                if (product.getResources().split(",").length > 1) {
                    if (!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[0])) {
                        sb.append("服务上架百度AI审核图片失败！图片地址为：" + product.getResources() + ";");
                    }
                } else {
                    if (!BaiduAuditUtils.ImgCensor(product.getResources())) {
                        sb.append("服务上架百度AI审核图片失败！图片地址为：" + product.getResources() + ";");
                    }
                }
                if (sb.length() > 0) {
                    ProductOnsaleAuditLog productOnsaleAuditLog = new ProductOnsaleAuditLog();
                    productOnsaleAuditLog.setAuditTime(date);
                    productOnsaleAuditLog.setProductId(id);
                    productOnsaleAuditLog.setNote(sb.toString());
                    productOnsaleAuditLog.setState(0);
                    productOnsaleAuditLogMapper.add(productOnsaleAuditLog);
                    continue;
                }
                product.setState(1L);
                product.setOnsaletime(date);
                productMapper.update(product);
//                FreemarkHtml.staticHtml(productPath, product, product.getId());
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        String productPath = path + "\\productDetail";
        ids.forEach(id->{
            String str = productPath + "\\"+ id + ".html";
            File file = new File(str);
            file.delete();
        });
        productMapper.batchOffsale(ids);
    }

    @Override
    public void add(Product product) {
        product.setCreatetime(new Date());
        super.add(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        productDetailMapper.add(productDetail);
    }

    @Override
    public void delete(Long id) {
        Product product = productMapper.findById(id);
        ProductDetail detail = productDetailMapper.findByPid(product.getId());
        super.delete(id);
        productDetailMapper.delete(detail.getId());
    }

    @Override
    public void update(Product product) {
        super.update(product);
        product.getProductDetail().setProductId(product.getId());
        productDetailService.update(product.getProductDetail());
    }
}
